package com.hj.enroll.controller;


import com.hj.enroll.pojo.Admin;
import com.hj.enroll.pojo.User;
import com.hj.enroll.service.AdminService;
import com.hj.enroll.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@RestController
public class AdminController {

    @Autowired
    private AdminService adminService;
    @Autowired
    private UserService userService;

    @GetMapping("/toAdminLogin")
    public String toAdminLogin(){
        return "admin/login";
    }
    //管理员登录
    @RequestMapping("/login")
    public boolean adminLogin(@RequestBody Map<String,String> map, HttpSession session){
        Admin admin = adminService.loginAdmin(map.get("name"), map.get("password"));
        if (admin != null){

            session.setAttribute("loginAdmin",map.get("name"));
            //使用重定向防止表单重复提交
            /*return "redirect:/admin/adminMain.html";*/
            return true;

        }else {
            /*"admin/login";return*/
            return false;
        }

    }

    //查看所有管理员
    @RequestMapping("/findAllAdmin")
    public List<Admin> findAllad(){

        List<Admin> allAdmin = adminService.findAllAdmin();
        return allAdmin;
    }



    //查看所有报名人员
    @RequestMapping("/findAllUser")
    public List<User> findAllUser(){
        List<User> allUser = userService.findAllUser();
        return allUser;
    }

    //修改某个报名成员的信息
    @RequestMapping("/updateUser")
    public int updateUser(@RequestBody User user, HttpServletRequest request){
        int i = userService.updateUser(user);
        if (i == 1) {
            request.setAttribute("msg","修改成功！");
        } else {
            request.setAttribute("msg","修改失败！");
        }
        return i;
    }

    @RequestMapping("/searchUser")
    //根据姓名查询某个成员
    public User searchUser(String name){
        User user = userService.searchUser(name);
        return user;
    }

    @RequestMapping("/updateAdmin")
    //修改管理员（自己）的信息
    public String updateAdmin(Admin admins){
        Admin admin = adminService.updateAdmin(admins);
        return null;
    }

    @RequestMapping("/addComment")
    //添加面评
    public String addComment(String name,String comment){
        int i = userService.addComment(name, comment);
        return null;
    }

    @RequestMapping("/deleteUser")
    public String deleteUser(String name){
        int i = userService.deleteUser(name);
        return "i"+i;
    }

}
